<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 16:38
 */

namespace app\api\controller\v1;

use app\api\model\Product as ProductModel;
use app\api\validate\count;
use app\api\validate\IDMustBePostiveInt;
use app\lib\exception\ProductException;


class Product
{
    public function getRecent($count=15)
    {
        (new count())->goCheck();
        $products = ProductModel::getMostRecent($count);
        if ($products->isEmpty())
        {
            throw new ProductException();
        }
        $products = $products->hidden(['summary']);
        return $products;
    }

    public function getAllCategory($id)
    {
        (new IDMustBePostiveInt())->goCheck();
        $result = ProductModel::where('category_id', '=', $id)->select();
        if ($result->isEmpty()){
            throw new ProductException();
        }
        $result = $result->hidden(['summary']);
        return $result;
    }
    public function getOne($id)
    {
        (new IDMustBePostiveInt())->goCheck();
        $result = ProductModel::getProductDetail($id);
        if (!$result){
            throw new ProductException();
        }
        $result = $result->hidden(['summary']);
        return $result;
    }
}